package com.gpay.erp.services.system.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.Dept;
import com.gpay.erp.core.system.bean.SysUser;
import com.gpay.erp.services.system.DeptInteface;
import org.springframework.stereotype.Service;

/**
 * 用户业务逻辑实现类
 * 
 * @author comrose
 * 
 */
@Service
public class DeptService implements DeptInteface {

	@Resource
	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public int insert(PagerModel e) {
		return dao.insert("dept.addDept", e);
	}

	public int delete(PagerModel e) {
		return dao.delete("dept.deleteById", e);
	}

	public int deletes(String[] ids) {
	if(ids!=null){
		int s=0;
		int flag;
	for (String id : ids) {
		Dept dept=new Dept();
		dept.setId(id);
		flag=(Integer) dao.selectOne("dept.usersAll", dept);
		s=flag+s;
	}
	if(s==0){
		for (String id : ids) {
			Dept dept=new Dept();
			dept.setId(id);
		   dao.delete("dept.deleteById", dept);
		}}else{
			return 0;
	}}
    return 0;
	}
	public int update(String[] ids,String name) {
		if(ids!=null){
			int s=0;
			int flag;
			for (String id : ids) {
				Dept dept=new Dept();
				dept.setId(id);
				flag=(Integer) dao.selectOne("dept.usersAll", dept);
				s=flag+s;
			}
			if(s==0){
			for (String id : ids) {
				Dept dept=new Dept();
				/* 设置日期格式 */
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				/* 获取当前是时间 */
				dept.setUpdateDate(df.format(new Date()));
				dept.setId(id);
				dept.setStatus("1");
				dept.setUpdateName(name);
				dao.update("dept.updateDept", dept);
				}
			return 1;
			}else{
					return -1;
			}}
		return 0;
	}
	
	public int update(Dept e) {
		if(e!=null){
			if(e.getStatus().equals("1")){
				int flag=(Integer) dao.selectOne("dept.usersAll", e);
			  if(flag==0){
				  return dao.update("dept.updateDept", e);
			  }else 
			  {
				  return -1;
			  }
			}else{
				return dao.update("dept.updateDept", e);	
			}			
		}
		return 0;
	}

	public PagerModel selectOne(PagerModel e) {
		return (PagerModel) dao.selectOne("dept.selectDataOne", e);
	}

	public PagerModel selectById(String id) {
		return (PagerModel) dao.selectOne("dept.selectOneNew", id);
	}

	public PagerModel selectPageList(PagerModel e) {
		return dao.selectPageList("dept.selectPageList", "dept.dataTotal", e);
	}

	public List selectList(PagerModel e) {
		return dao.selectList("dept.selectList");
	}

	@Override
	public int update(PagerModel e) {
		return 0;
	}


}
